﻿using System;
using Utils;
using Utils.Wpf;

namespace StockAnalyst2.Chart
{
	internal class ChartToolBarVM : ViewModelBase
	{
		#region Attributes

		private bool _isRealTimeEnabled;
		private string _selectedStockName;
		private DateTime? _selectedDate;
		private DateTime _firstDate;
		private DateTime _lastDate;

		#endregion

		#region Properties

		public bool IsRealTimeEnabled
		{
			get
			{
				return _isRealTimeEnabled;
			}
			set
			{
				if (_isRealTimeEnabled != value)
				{
					_isRealTimeEnabled = value;
					this.StockNames.Clear();
					base.OnPropertyChanged("IsRealTimeEnabled");
					base.OnPropertyChanged("AreParametersOkToRefreshDays");
				}
			}
		}

		public string SelectedStockName
		{
			get
			{
				return _selectedStockName;
			}
			set
			{
				if (_selectedStockName != value)
				{
					_selectedStockName = value;
					base.OnPropertyChanged("SelectedStockName");
					base.OnPropertyChanged("AreParametersOkToRefreshDays");

					if (!this.IsRealTimeEnabled && this.ChartCommandsVM.RefreshAvailableDaysCommand.CanExecute(null))
					{
						this.ChartCommandsVM.RefreshAvailableDaysCommand.Execute(null);
					}
				}
			}
		}

		public DateTime FirstDate
		{
			get
			{
				return _firstDate;
			}
			set
			{
				if (_firstDate != value)
				{
					_firstDate = value;
					base.OnPropertyChanged("FirstDate");
				}
			}
		}

		public DateTime LastDate
		{
			get
			{
				return _lastDate;
			}
			set
			{
				if (_lastDate != value)
				{
					_lastDate = value;
					base.OnPropertyChanged("LastDate");
				}
			}
		}

		public DateTime? SelectedDate
		{
			get
			{
				return _selectedDate;
			}
			set
			{
				if (_selectedDate != value)
				{
					_selectedDate = value;
					base.OnPropertyChanged("SelectedDate");
				}
			}
		}

		public bool AreParametersOkToRefreshDays
		{
			get
			{
				return !this.IsRealTimeEnabled && this.SelectedStockName != null;
			}
		}

		public ObservableCollection2<string> StockNames { get; private set; }
		public ObservableCollection2<DateTime> DaysWithoutData { get; private set; }

		public ChartCommandsVM ChartCommandsVM { get; set; }

		#endregion

		#region Public Methods

		public ChartToolBarVM()
		{
			this.StockNames = new ObservableCollection2<string>();
			this.DaysWithoutData = new ObservableCollection2<DateTime>();
		}

		#endregion
	}
}
